package mo.tags.stack;

import java.util.Stack;

public class L1673 {

    public int[] mostCompetitive(int[] nums, int k) {
        Stack<Integer> stack = new Stack<>();
        stack.add(-1);
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (nums[i] < stack.peek() && k - stack.size() + 1 < n - i) {
                stack.pop();
            }
            if (stack.size() <= k) {
                stack.push(nums[i]);
            }
        }
        int[] res = new int[k];
        while (k > 0) {
            res[--k] = stack.pop();
        }
        return res;
    }

}
